package com.swf.seed.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

public class ShutdownHookBean implements Runnable, BeanFactoryAware {
	private ConfigurableListableBeanFactory factory;
	@Override
	public void setBeanFactory(BeanFactory factory) throws BeansException {
		if (factory instanceof ConfigurableListableBeanFactory){
			this.factory = (ConfigurableListableBeanFactory)factory;
			Runtime.getRuntime().addShutdownHook(new Thread(this));
		}

	}

	@Override
	public void run() {
		if (factory != null){
			System.out.println("Destorying Singletons");
			factory.destroySingletons();
			System.out.println("Singletons Desotryed");
		}

	}

}
